package com.xunchi.haichuan.user.action;

import org.apache.commons.lang.StringUtils;

import com.netai.safedoc.utility.TransactionWrapper;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.xunchi.haichuan.user.model.User;
import com.xunchi.haichuan.user.service.UserService;
import com.xunchi.haichuan.user.service.UserServiceImpl;
import com.xunchi.haichuan.utility.Digest;

/**
 * 作者：kongxb<br>
 * 模块名称：用户模块<br>
 * 功能描述：用户管理<br>
 * 维护记录：2009.12.17, kongxb 创建该类
 */
public class UserManageAction extends ActionSupport {

	private static final long serialVersionUID = -8679736919515665120L;

	private transient String usrId;
	private transient int status;
	private transient String oldPassword;
	private transient String password;
	private transient String password2;
	private transient float diff;

	private boolean success = true;
	private String msg = "";

	private UserService userService;

	public String execute() throws Exception {
		return Action.SUCCESS;
	}

	/**
	 * 更改用户的状态
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changeStatus() throws Exception {
		userService = (UserService) TransactionWrapper.decorate(new UserServiceImpl(), true);
		userService.changeStatus(usrId, status);
		return Action.SUCCESS;
	}

	/**
	 * 更改用户的密码
	 * 
	 * @return
	 * @throws Exception
	 */
	public String changePwd() throws Exception {
		userService = (UserService) TransactionWrapper.decorate(new UserServiceImpl(), true);
		if (StringUtils.isNotEmpty(usrId)) {
			User user = userService.getUserById(usrId);
			if (StringUtils.isEmpty(password) || !StringUtils.equals(password, password2)) {
				success = false;
				msg = this.getText("USER.error.changeUserInfo.newPassword");
			} else if (!StringUtils.equals(Digest.getSHA1Digest(oldPassword), user.getLoginPassword())) {
				success = false;
				msg = this.getText("USER.error.changeUserInfo.password");
			} else {
				userService.changePwd(usrId, Digest.getSHA1Digest(password), null);
			}
		}
		return Action.SUCCESS;
	}

	/**
	 * 充值/扣费
	 * 
	 * @return
	 * @throws Exception
	 */
	public String charge() throws Exception {
		userService = (UserService) TransactionWrapper.decorate(new UserServiceImpl(), true);
		userService.charge(usrId, diff);
		return Action.SUCCESS;
	}

	public boolean isSuccess() {
		return success;
	}

	public String getMsg() {
		return msg;
	}

	public void setUsrId(String usrId) {
		this.usrId = usrId;
	}

	public void setStatus(String status) {
		this.status = Integer.parseInt(status);
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public void setDiff(String diff) {
		this.diff = Float.parseFloat(diff);
	}

}
